//===============================================================================
// XGenPlus Tool
// ==============================================================================
// Please visit http://www.codeplex.com/xgenplus for latest updates.
//
// This source is subject to the GNU General Public License v2.
// 
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using XGenPlus;
using XGenPlus.Config;
using System.IO;
using System.Reflection;
using System.Runtime.Remoting;

public class XGenPlusTask : Microsoft.Build.Utilities.Task
{
    Parameters p = new Parameters();

    [Required]
    public string AssemblyName
    {
        get { return p.AssemblyName; }
        set { p.AssemblyName = value; }
    }

    public string From
    {
        get { return p.From; }
        set { p.From = value; }
    }

    public string GetConfig
    {
        get { return p.GetConfig; }
        set { p.GetConfig = value; }
    }

    public string CopyTo
    {
        get { return p.CopyTo; }
        set { p.CopyTo = value; }
    }

    public bool SerializeAll
    {
        get { return p.SerializeAll; }
        set { p.SerializeAll = value; }
    }

    public bool NoCompile
    {
        get { return p.NoCompile; }
        set { p.NoCompile = value; }
    }

    public bool NoGenerate
    {
        get { return p.NoGenerate; }
        set { p.NoGenerate = value; }
    }

    public string[] ExcludeList
    {
        get { return p.ExcludeList.ToArray(); }
        set
        {
            p.ExcludeList.Clear();
            string[] result = value;
            for (int i = 0; i < result.Length; i++)
                p.ExcludeList.Add(result[i]);

        }
    }

    public string[] IncludeList
    {
        get { return p.IncludeList.ToArray(); }
        set
        {
            p.IncludeList.Clear();
            string[] result = value;

            for (int i = 0; i < result.Length; i++)
                p.IncludeList.Add(result[i]);
        }
    }

    public string[] ReferenceList
    {
        get { return p.ReferenceList.ToArray(); }
        set
        {
            p.ReferenceList.Clear();
            string[] result = value;
            for (int i = 0; i < result.Length; i++)
                p.ReferenceList.Add(result[i]);
        }
    }

    private void WriteToLog(string s)
    {
        base.Log.LogMessage(s);
    }

    /// <summary>
    /// Method to perform the execution
    /// </summary>
    /// <returns></returns>
    public override bool Execute()
    {
        try
        {
            WriteToLog("Invoking XGenPlus Task..");

            if (null != p.GetConfig && p.GetConfig != "")
            {
                try
                {
                    p = XGenPlus.Config.Util.GetConfig(p.GetConfig);
                }
                catch (Exception e)
                {
                    Log.LogError("Unable to read from configuration file " + p.GetConfig);
                    return false;
                }
            }

            if (null == p.AssemblyName || p.AssemblyName == "")
            {
                Log.LogError("Please specify assembly name");
                return false;
            }

            XGenPlus.Logger log = new XGenPlus.Logger();
            log.MessageListener += new XGenPlus.Logger.MessageListenerDelegate(WriteToLog);

            bool result = XGenPlus.Generator.Runner.InvokeRunnerInOwnAppDomain(log, p);
            WriteToLog("XGenPlus Task Completed.");

            return result;

        }
        catch (Exception e)
        {
            WriteToLog("XGenPlus Task Failed.");
            Log.LogError("Error while generating typed serializers. \n" + e.Message);
            return false;
        }
    }
}
